<?php

/**
 * MyQEE 视图核心类
 *
 * @author	 jonwang(jonwang@myqee.com)
 * @category   MyQEE
 * @package	System
 * @subpackage Core
 * @copyright  Copyright (c) 2008-2011 myqee.com
 * @license	http://www.myqee.com/license.html
 */
class MyQEE_View
{

    protected $_file;

    protected $_data = array();

    protected static $_global_data = array();

    function __construct($file = null, array $data = null)
    {
        $this->set_filename($file);
        if ( is_array($data) )
        {
            $this->_data = $data;
        }
    }

    public function set_filename($file)
    {
        $realfile = Core::find_file('views', $file);
        
        if ( $realfile )
        {
            $this->_file = $realfile;
        }
        else
        {
            throw new Exception('指定的视图“' . $file . '”不存在');
        }
    }

    /**
     * 构造视图
     * @param string $file
     * @param array $data
     * @return View
     */
    public static function factory($file = NULL, array $data = NULL)
    {
        return new View($file, $data);
    }

    public function __set($key, $value)
    {
        $this->set($key, $value);
    }

    public function &__get($key)
    {
        return $this->_data[$key];
    }

    public function bind($key, & $value)
    {
        $this->_data[$key] = & $value;
        
        return $this;
    }

    /**
     * 设置变量
     * @param string $key
     * @param $value
     * @return View
     */
    public function set($key, $value = NULL)
    {
        if ( is_array($key) )
        {
            foreach ( $key as $name => $value )
            {
                $this->_data[$name] = $value;
            }
        }
        else
        {
            $this->_data[$key] = $value;
        }
        
        return $this;
    }

    /**
     * 设置全局视图变量
     *
     * @param string/array $key
     * @param mixed $value
     */
    public static function set_global($key, $value = null)
    {
        if ( is_array($key) )
        {
            foreach ( $key as $k => $v )
            {
                View::$_global_data[$k] = $v;
            }
        }
        else
        {
            View::$_global_data[$key] = $value;
        }
    }

    public function render($print = true)
    {
        
        if ( empty($this->_file) )
        {
            throw new Exception('You must set the file to use within your view before rendering');
        }
        
        // Combine local and global data and capture the output
        $output = View::capture($this->_file, $this->_data);
        
        if ( $print )
        {
            echo $output;
        }
        else
        {
            return $output;
        }
    }

    public function __toString()
    {
        try
        {
            return $this->render(false);
        }
        catch ( Exception $e )
        {
            return '';
        }
    }

    protected static function capture($myqee_view_filename, array $myqee_view_data)
    {
        // Import the view variables to local namespace
        extract($myqee_view_data, EXTR_SKIP);
        
        if ( View::$_global_data )
        {
            // Import the global view variables to local namespace and maintain references
            extract(View::$_global_data, EXTR_REFS);
        }
        
        // Capture the view output
        ob_start();
        
        try
        {
            // Load the view within the current scope
            include $myqee_view_filename;
        }
        catch ( Exception $e )
        {
            // Delete the output buffer
            ob_end_clean();
            
            // Re-throw the exception
            throw $e;
        }
        
        // Get the captured output and close the buffer
        return ob_get_clean();
    }
}
